﻿package mortal.game.scene3D.model.player
{
    import __AS3__.vec.*;
    import baseEngine.basic.*;
    import baseEngine.core.*;
    import baseEngine.modifiers.*;
    import baseEngine.system.*;
    import com.gengine.resource.info.*;
    import frEngine.*;
    import frEngine.manager.*;
    import frEngine.pools.*;
    import mortal.game.scene3D.layer3D.utils.*;
    import mortal.game.scene3D.model.pools.*;

    public class GroupEffectPlayer extends Pivot3D implements IGame2D
    {
        protected var _curUrl:String;
        protected var _loaded:Boolean = false;
        private var _toPlayList:Vector.<PlayInfo>;
        private var _renderList:RenderList;
        protected var _y2d:Number = 0;
        protected var _x2d:Number = 0;
        protected var _direction:Number = 0;
        protected var _animationMode:int = 1;
        private var _isPlaying:Boolean = true;
        private var _maxFrame:int = 1000000;
        private var _clearEnabled:Boolean;
        private var _timeKey:int;
        private var _timeControler:TimeControler;

        public function GroupEffectPlayer(param1:String = null)
        {
            this._toPlayList = new Vector.<PlayInfo>;
            super(param1);
            var _loc_2:* = Global3D;
            _loc_2.timeKeyId = Global3D.timeKeyId + 1;
            this._timeKey = Global3D.timeKeyId + 1;
            var _loc_2:* = TimeControler.createTimerInstance(null, null, this._timeKey);
            this._timeControler = TimeControler.createTimerInstance(null, null, this._timeKey);
            this.timerContorler = _loc_2;
            this.stop();
            return;
        }// end function

        public function play(param1:int = 0) : void
        {
            this._animationMode = param1;
            this._isPlaying = true;
            this.timerContorler.active(this._timeKey);
            this.timerContorler.gotoFrame(0, 0);
            return;
        }// end function

        public function stop() : void
        {
            var _loc_3:PlayInfo = null;
            this._isPlaying = false;
            this.timerContorler.unActive(this._timeKey);
            this.timerContorler.gotoFrame(0, 0);
            var _loc_1:* = this._toPlayList.length;
            var _loc_2:int = 0;
            while (_loc_2 < _loc_1)
            {
                
                _loc_3 = this._toPlayList[_loc_2];
                _loc_3.stopPlay();
                _loc_2++;
            }
            return;
        }// end function

        public function set direction(param1:Number) : void
        {
            this._direction = param1;
            this.setRotation(0, Scene3DUtil.change2Dto3DRotation(this._direction), 0);
            return;
        }// end function

        public function get direction() : Number
        {
            return this._direction;
        }// end function

        public function set x2d(param1:Number) : void
        {
            this._x2d = param1;
            x = Scene3DUtil.change2Dto3DX(param1);
            return;
        }// end function

        public function get x2d() : Number
        {
            return this._x2d;
        }// end function

        public function set y2d(param1:Number) : void
        {
            this._y2d = param1;
            z = Scene3DUtil.change2Dto3DY(param1);
            return;
        }// end function

        public function get y2d() : Number
        {
            return this._y2d;
        }// end function

        public function load(param1:String, param2:Boolean, param3:RenderList = null, param4:Pivot3D = null) : void
        {
            this._renderList = param3;
            this._maxFrame = int.MAX_VALUE;
            this._clearEnabled = param2;
            if (!this._renderList)
            {
                this._renderList = Global3D.scene.renderLayerList;
            }
            if (this._curUrl != param1)
            {
                this._curUrl = param1;
                Resource3dManager.instance.load(this._curUrl, this.loadedHander, 1);
            }
            this.parent = param4;
            return;
        }// end function

        override public function addedToScene(param1:Scene3D) : Boolean
        {
            var _loc_2:* = super.addedToScene(param1);
            return _loc_2;
        }// end function

        override public function removedFromScene() : void
        {
            super.removedFromScene();
            this.timerContorler.unActive(this._timeKey);
            return;
        }// end function

        protected function loadedHander(param1:XMLInfo, param2:XML = null) : void
        {
            var _loc_7:XML = null;
            this._loaded = true;
            if (param1)
            {
                param2 = param1.xml;
            }
            this.disposePlayer();
            this.timerContorler.gotoFrame(0, 0);
            var _loc_3:* = param2.nodes[0];
            this._maxFrame = int(_loc_3.@playEnd);
            var _loc_4:* = _loc_3.node;
            var _loc_5:* = _loc_4.length();
            var _loc_6:int = 0;
            while (_loc_6 < _loc_5)
            {
                
                _loc_7 = _loc_4[_loc_6];
                if (_loc_7.@type == "effect")
                {
                    this.createEeffect(String(_loc_7.@url), int(_loc_7.@startFrame), String(_loc_7.@isCirclePlay) == "true", String(_loc_7.@pos), String(_loc_7.@scale), String(_loc_7.@rot));
                }
                _loc_6++;
            }
            return;
        }// end function

        private function disposePlayer() : void
        {
            var _loc_3:PlayInfo = null;
            var _loc_1:* = this._toPlayList.length;
            var _loc_2:int = 0;
            while (_loc_2 < _loc_1)
            {
                
                _loc_3 = this._toPlayList[_loc_2];
                _loc_3.dispose();
                _loc_2++;
            }
            this._toPlayList = new Vector.<PlayInfo>;
            return;
        }// end function

        override public function update(param1:int = 0) : void
        {
            var _loc_4:int = 0;
            var _loc_5:PlayInfo = null;
            if (!this._isPlaying)
            {
                return;
            }
            var _loc_2:* = this.timerContorler.totalframe;
            var _loc_3:* = this._toPlayList.length;
            if (_loc_2 > this._maxFrame)
            {
                this.stop();
                if (this._animationMode == PlayMode.ANIMATION_LOOP_MODE)
                {
                    this.play();
                }
                else if (this._animationMode == PlayMode.ANIMATION_STOP_MODE)
                {
                    this.dispose();
                    return;
                }
            }
            else
            {
                _loc_4 = 0;
                while (_loc_4 < _loc_3)
                {
                    
                    _loc_5 = this._toPlayList[_loc_4];
                    if (!_loc_5.isPlaying && _loc_2 >= _loc_5.startFrame)
                    {
                        _loc_5.startPlay(this, this._renderList);
                    }
                    _loc_4++;
                }
            }
            super.update(param1);
            return;
        }// end function

        private function createEeffect(param1:String, param2:int, param3:Boolean, param4:String = null, param5:String = null, param6:String = null) : void
        {
            var _loc_9:Array = null;
            var _loc_7:* = EffectPlayerPool.instance.getEffectPlayer(param1, this, null, this._clearEnabled);
            _loc_7.enabledPool = false;
            var _loc_8:* = new PlayInfo(param2, _loc_7, param3);
            this._toPlayList.push(_loc_8);
            if (param4)
            {
                _loc_9 = param4.split(",");
                _loc_7.x = int(_loc_9[0]);
                _loc_7.y = int(_loc_9[1]);
                _loc_7.z = int(_loc_9[2]);
            }
            if (param5)
            {
                _loc_9 = param5.split(",");
                _loc_7.scaleX = Number(_loc_9[0]);
                _loc_7.scaleY = Number(_loc_9[1]);
                _loc_7.scaleZ = Number(_loc_9[2]);
            }
            if (param6)
            {
                _loc_9 = param6.split(",");
                _loc_7.rotationX = int(_loc_9[0]);
                _loc_7.rotationY = int(_loc_9[1]);
                _loc_7.rotationZ = int(_loc_9[2]);
            }
            return;
        }// end function

        override public function dispose(param1:Boolean = true) : void
        {
            this.disposePlayer();
            super.dispose(false);
            this.timerContorler = this._timeControler;
            this._renderList = null;
            this.stop();
            this._loaded = false;
            this._curUrl = null;
            Resource3dManager.instance.unLoad(this._curUrl, this.loadedHander);
            if (param1)
            {
                FrObjectPool.disposeObject(this, GroupEffectPlayer);
            }
            return;
        }// end function

    }
}

class PlayInfo extends Object
{
    public var startFrame:int;
    public var player:EffectPlayer;
    public var repeatPlay:Boolean;
    public var isPlaying:Boolean = false;

    function PlayInfo(param1:int, param2:EffectPlayer, param3:Boolean)
    {
        this.startFrame = param1;
        this.player = param2;
        this.repeatPlay = param3;
        return;
    }// end function

    public function startPlay(param1:Pivot3D, param2:RenderList) : void
    {
        var _loc_3:* = this.repeatPlay ? (PlayMode.ANIMATION_LOOP_MODE) : (PlayMode.ANIMATION_STOP_MODE);
        this.player.play(_loc_3);
        this.player.renderList = param2;
        param1.addChild(this.player);
        this.isPlaying = true;
        return;
    }// end function

    public function stopPlay() : void
    {
        this.player.stop();
        this.player.parent = null;
        this.isPlaying = false;
        return;
    }// end function

    public function dispose() : void
    {
        if (this.player)
        {
            this.player.enabledPool = true;
            this.player.dispose();
            this.isPlaying = false;
            this.player = null;
        }
        return;
    }// end function

}

